# _*_ coding: UTF-8 _*_
# @Time     : 2021/2/20 上午 11:42
# @Author   : Li Jie
# @Site     : http://www.hzdledu.com/
# @File     : udp_server.py
# @Software : PyCharm

import socket

host = '127.0.0.1'
port = 8888
serverSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)  # 创建UDP server socket
serverSocket.bind((host, port))  # 绑定ip和port
print(f'绑定{host}:{port}')
recv_data, addr = serverSocket.recvfrom(1024)  # 接收数据
while recv_data.decode() != 'bye':  # 内容不为'bye'就一直循环
    if recv_data:
        print(f'接收到{addr}的内容：{recv_data.decode()}')  # 打印接收的信息
    send_data = input(f'请输入发送的内容:')  # 输入发送信息
    serverSocket.sendto(send_data.encode(), addr)  # 发送信息
    if send_data == 'bye':  # 如果发送信息为‘bye’，退出循环
        break
    recv_data, addr = serverSocket.recvfrom(1024)  # 接收信息
serverSocket.close()  # 关闭server socket
